home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xsok-1.000 / xsok-1 / xsok-1.01 / src / messages.c < prev    next >
C/C++ Source or Header  |  1995-10-14  |  8KB  |  303 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    Xsok version 1.01 -- module messages.c                     */
  5. /*                                         */
  6. /*    Internationalisation and keyboard translation/customisation.         */
  7. /*    Written by Michael Bischoff (mbi@mo.math.nat.tu-bs.de)             */
  8. /*    November-1994                                 */
  9. /*    see COPYRIGHT.xsok for Copyright details                 */
  10. /*                                         */
  11. /*                                         */
  12. /*****************************************************************************/
  13. #ifndef _POSIX_SOURCE
  14. #define _POSIX_SOURCE
  15. #endif
  16. #include "xsok.h"
  17. #include <ctype.h>
  18.  
  19. void (*lastcmd)(void);
  20. int numeric_arg = 0;
  21.  
  22. const char *xsok_messages[] = {
  23.     "Quit game?",
  24.     "Another game?",
  25.     "Restart game?",
  26.     "Next level?",
  27.     "Previous level?",
  28.     "Not possible",
  29.     "Bookmark set",
  30.     "You won! (Score %d, Best score: %d)",
  31.     "OK",
  32.     "XSok version %s",
  33.     "Moves: %d, Pushes: %d, Score: %d. Current strength: %d.",
  34.     "Already at starting position.",
  35.     "Move undone.",
  36.     "Redo not possible.",
  37.     "Move redone.",
  38.     "Welcome to XSok version %s!",
  39.  
  40.     "Saving of game FAILED.",
  41.     "Loading the game FAILED.",
  42.     "Could not open file.",
  43.     "Could not open file.",
  44.     "Could not write header.",
  45.     "Could not read header.",
  46.     "Could not write moves.",
  47.     "Could not read moves.",
  48.     "Saving the game succeeded.",
  49.     "Loading the game succeeded.",
  50.     "Magic match failed.",
  51.     
  52.     "The author of this level is unknown.",
  53.     "New record for this level!",
  54.     "Cannot find a savegame file.",
  55.     "Help on keys",
  56.     "Help on the %s level subset",
  57.  
  58.     "Starting macro recording.",
  59.     "Recorded macro has %d moves.",
  60.     "Wrong position for macro replay.",
  61.  
  62.     "Proceed to the next unsolved level?",
  63.     "Least moves: %d, Least pushes: %d, Best score: %d.",
  64.     "This level is unsolved.",
  65.     "Cannot link savegame files.",
  66. };
  67.  
  68. void read_message_file(const char *filename) {
  69.     FILE *fp;
  70.     int i;
  71.     char line[256];
  72.     if (*filename != '/') {
  73.     sprintf(line, "%s/%s/%s", xsokdir, langdir, filename);
  74.     filename = line;
  75.     }
  76.     if (!(fp = fopen(filename, "r")))
  77.     return;        /* Xt philosophy: ignore error */
  78.     for (i = 0; i < sizeof(xsok_messages) / sizeof(const char *); ++i) {
  79.     char *p;
  80.     again:
  81.     if (!fgets(line, sizeof(line), fp))
  82.         break;    /* EOF */
  83.     if (*line == '#')
  84.         goto again;
  85.     if ((p = strrchr(line, '\n')))
  86.         *p = '\0';
  87.     if (*line)    /* empty lines => keep old text */
  88.         xsok_messages[i] = strsav(line);
  89.     }
  90.     fclose(fp);
  91. }
  92.  
  93. static void cmd_None(void) {}
  94.  
  95. static void replace_binding(struct key_action *p, const char *name) {
  96.     int i;
  97.     static struct translator {
  98.     const char *name;
  99.     void (*func)(void);
  100.     } translator[] = {
  101.     { "None",        cmd_None },
  102.     { "rq_LeaveSok",    rq_LeaveSok },
  103.     { "rq_RestartGame",    rq_RestartGame },
  104.     { "rq_NextLevel",    rq_NextLevel },
  105.     { "rq_NextUnsolved",    rq_NextUnsolved },
  106.     { "rq_PrevLevel",    rq_PrevLevel },
  107.     { "Up",            cmd_Up },
  108.     { "Left",        cmd_Left },
  109.     { "Down",        cmd_Down },
  110.     { "Right",        cmd_Right },
  111.     { "NextUnsolved",    cmd_NextUnsolved },
  112.     { "NextLevel",        cmd_NextLevel },
  113.     { "PrevLevel",        cmd_PrevLevel },
  114.     { "UndoMove",        cmd_UndoMove },
  115.     { "RedoMove",        cmd_RedoMove },
  116.     { "LeaveSok",        cmd_LeaveSok },
  117.     { "ShowScore",        cmd_ShowScore },
  118.     { "ShowBestScore",    cmd_ShowBestScore },
  119.     { "ShowAuthor",        cmd_ShowAuthor },
  120.     { "RestartGame",    cmd_RestartGame },
  121.     { "ReplayGame",        cmd_ReplayGame },
  122.     { "SaveGame",        cmd_SaveGame },
  123.     { "LoadGame",        cmd_LoadGame },
  124.     { "ReadScores",        cmd_ReadHighscores },
  125.     { "ShowVersion",    cmd_ShowVersion },
  126.     { "ResizeWindow",    cmd_Resize },
  127.     { "RepeatMove",        cmd_Repeat },
  128.     { "MouseMove",        cmd_MouseMove },    /* requires x, y */
  129.     { "MousePush",        cmd_MousePush },    /* requires x, y */
  130.     { "MouseUndo",        cmd_MouseUndo },
  131.     { "LevelInfo",        cmd_LevelInfo },
  132.     { "DropBookmark",    cmd_DropBookmark },
  133.     { "GotoBookmark",    cmd_GotoBookmark },
  134.     { "StartMacro",        cmd_StartMacro },
  135.     { "EndMacro",        cmd_EndMacro },
  136.     { "PlayMacro",        cmd_PlayMacro },
  137. /*    { "Debug",        cmd_debug }, */
  138.     { "Cancel",        cmd_Cancel },
  139.     { "Confirm",        cmd_Confirm }
  140.     };
  141.     for (i = 0; i < sizeof(translator) / sizeof(translator[0]); ++i)
  142.     if (!strcmp(name, translator[i].name)) {
  143.         p->action = translator[i].func;
  144.         return;
  145.     }
  146.     fprintf(stderr, "WARNING: no function corresponds to \"%s\"\n", name);
  147.     p->action = cmd_None;
  148. }
  149.  
  150. static struct key_action *global_bindings = NULL;
  151. const char *langdir = "";
  152.  
  153. void add_keybinding(struct key_action **cp, const char *cmd, const char *function) {
  154.     /* a NULL pointer for cmd is a wildcard */
  155.     int done = 0;
  156.     struct key_action *p;
  157.  
  158.     if (!cp)
  159.     return;        /* bindings for unimplemented rulesets */
  160.     while (*cp) {
  161.     p = *cp;
  162.     if (!cmd || !strcmp(p->string, cmd)) {
  163.         /* replace this! */
  164.         replace_binding(p, function);
  165.         done = 1;
  166.     }
  167.     cp = &(p->next);
  168.     }
  169.     if (!done && cmd) {
  170.     /* didn't find previous command */
  171.     /* add a new entry */
  172.     p = *cp = malloc_(sizeof(struct key_action));
  173.     p->next = NULL;
  174.     p->string = strsav(cmd);
  175.     replace_binding(p, function);
  176.     }
  177. }
  178.  
  179.  
  180. void read_keyboard_file(const char *filename) {
  181.     FILE *fp;
  182.     char line[256];
  183.     char buff[32], cmd[2];
  184.     struct key_action **cp = &global_bindings;
  185.  
  186.     cmd[1] = '\0';    /* 1-char commands currently */
  187.     if (*filename != '/') {
  188.     sprintf(line, "%s/%s/%s", xsokdir, langdir, filename);
  189.     filename = line;
  190.     }
  191.     /* printf("reading keyboard file \"%s\"\n", filename); */
  192.     if (!(fp = fopen(filename, "r"))) {
  193.     /* in xsok, the keyboard file is required, not optional */
  194.     /* therefore, we issue an error */
  195.     fprintf(stderr, "Cannot read keyboard definition file %s\n",
  196.         filename);
  197.     if (*langdir)
  198.         fprintf(stderr, "Hint: Perhaps unsetting LANG or making a symbolic"
  199.             " link\nfrom %s/%s to %s helps.\n", xsokdir,
  200.             langdir, xsokdir);
  201.     exit(1);
  202.     }
  203.     while (fgets(line, sizeof(line), fp)) {
  204.     char *p;
  205.     if ((p = strrchr(line, '\n')))
  206.         *p = '\0';
  207.     if (!*line)
  208.         continue;
  209.     if (!strncmp(line, "#c", 2))    /* comment */
  210.         continue;
  211.     if (!strncmp(line, "#include ", 9)) {    /* include */
  212.         read_keyboard_file(line+9);
  213.         continue;
  214.     }
  215.     if (!strncmp(line, "#x", 2) && strlen(line) < 32) { /* hex number */
  216.         int c;
  217.         sscanf(line+2, "%x %s", &c, buff);
  218.         cmd[0] = c;
  219.         add_keybinding(cp, cmd, buff);
  220.         continue;
  221.     }
  222.     /* else assume string<space>commandname */
  223.     if (strlen(line) >= 3 && strlen(line) < 32) {
  224.         p = line+1;
  225.         while (*p && *p != '\t' && *p != ' ')
  226.         ++p;
  227.         if (*p) {
  228.         *p = '\0';
  229.         while (*++p && (*p == '\t' || *p == ' '))
  230.             ;
  231.         if (*p) {
  232.             add_keybinding(cp, line, p);
  233.             continue;
  234.         }
  235.         }
  236.     }
  237.     fprintf(stderr, "Warning: cannot parse line in keys file:%s\n", line);
  238.     }
  239.     fclose(fp);
  240. }
  241.  
  242. void key_pressed(char *str) {
  243.     struct key_action *p;
  244.  
  245.     /* search for global binding */
  246.     for (p = global_bindings; p; p = p->next)
  247.     if (!strcmp(p->string, str)) {
  248.         (*p->action)();
  249.         lastcmd = p->action;
  250.         numeric_arg = 0;
  251.         return;
  252.     }
  253.  
  254.     /* not found. break up the string to smaller pieces */
  255.     if (strlen(str) > 1) {
  256.     char s[2];
  257.     while (*str) {
  258.         s[0] = *str++;
  259.         s[1] = '\0';
  260.         key_pressed(s);
  261.     }
  262.     return;
  263.     }    /* use hardcoded entries. Note: These can be overridden by assigning them "None" before. */
  264.     
  265.     /* process digits */
  266.     if (isdigit(str[0])) {
  267.     if (numeric_arg >= 3267)
  268.         numeric_arg = 0;
  269.     numeric_arg = 10 * numeric_arg + (str[0] - '0');
  270.     show_message("%d", numeric_arg);
  271.     return;
  272.     }
  273.     switch (str[0]) {
  274.     case '\b':
  275.     if (numeric_arg >= 10) {
  276.         show_message("%d", numeric_arg /= 10);
  277.         return;
  278.     }
  279.     /* else fall through */
  280.     case '\033':    /* esc */
  281.     numeric_arg = 0;
  282.     show_message(" ");
  283.     return;
  284.     case '\014':    /* ctrl-L */
  285.     case '\022':    /* ctrl-R */
  286.     refresh_screen();
  287.     return;
  288.     }
  289.     if (str[0]) {
  290.     const char *rulechange = "XCS";
  291.     const char *rulename[] = { "Xsok", "Cyberbox", "Sokoban" };
  292.     const char *s;
  293.  
  294.     if ((s = strchr(rulechange, str[0]))) {
  295.         /* printf("found %s\n", rulename[s-rulechange]); */
  296.         change_rules(rulename[s-rulechange]);
  297.         NewLevel(1);
  298.         cmd_LevelInfo();
  299.         return;
  300.     }
  301.     }
  302. }
  303.